package com.ph.monitorPlatform.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ph.monitorPlatform.dto.TaskInfoExportDto;
import com.ph.monitorPlatform.entity.TaskInfo;
import com.ph.monitorPlatform.mapper.TaskInfoMapper;
import com.ph.monitorPlatform.service.ITaskInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zuohongyu
 * @since 2020-03-18
 */
@Service
public class TaskInfoServiceImpl extends ServiceImpl<TaskInfoMapper, TaskInfo> implements ITaskInfoService {

    @Autowired
    private TaskInfoMapper taskInfoMapper;
    @Autowired
    private ExportToExcel exportToExcel;

    @Override
    public void exportTasks(List<BigDecimal> taskIds, HttpServletResponse response) throws IOException {
        List<TaskInfoExportDto> taskInfoExportDtos = taskInfoMapper.selectByTaskIds(taskIds);
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        taskInfoExportDtos.stream().forEach(a -> {
            a.setTaskType(transTypeToString(a.getTaskType()));
            a.setPlanTime(sdf.format(a.getPlanStartTime()) + "-" + sdf.format(a.getPlanEndTime()));
            //ascription状态转换
            a.setAscription(transAscriptionToString(a.getAscription()));
            //任务单状态转换
            a.setTaskStatus(transStatusToString(a.getTaskStatus()));
            //state状态转换
            a.setTaskState(transStateToString(a.getTaskState()));
            //is_delay
            a.setIsDelay(transDelayToString(a.getIsDelay()));
            //是否是默认执行人
            a.setIsDfTransactor(transDelayToString(a.getIsDfTransactor()));
        });
        exportToExcel.exportToExcel(response, taskInfoExportDtos, "巡检任务单", TaskInfoExportDto.class);

    }

    public String transTypeToString(String taskType) {
        String taskTypeString;
        switch (taskType) {
            case "A":
                taskTypeString = "例行巡检";
                break;
            default:
                taskTypeString = "unknown";
        }
        return taskTypeString;
    }

    public String transAscriptionToString(String ascription) {
        String ascriptionString;
        switch (ascription) {
            case "MachineRoom-BJ":
                ascriptionString = "北京机房";
                break;
            case "MachineRoom-XM":
                ascriptionString = "厦门机房";
                break;
            default:
                ascriptionString = "unknown";
        }
        return ascriptionString;
    }


    public String transStatusToString(String status) {
        String statusString = "";
        switch (status) {
            case "A":
                statusString = "未执行";
                break;
            case "B":
                statusString = "已下载";
                break;
            case "D":
                statusString = "已完成";
                break;
            case "E":
                statusString = "超时完成";
                break;
            case "F":
                statusString = "失败";
                break;
        }
        return statusString;
    }


    public String transStateToString(String taskState) {
        String taskStateString = "";
        if (StringUtils.isNotBlank(taskState)) {
            switch (taskState) {
                case "Y":
                    taskStateString = "否";
                    break;
                case "N":
                    taskStateString = "是";
                    break;
            }
        }
        return taskStateString;
    }

    //isDelay + isDfTranstrator
    public String transDelayToString(String isDelay) {
        String isDelayString = "";
        if (StringUtils.isNotBlank(isDelay)) {
            switch (isDelay) {
                case "0":
                    isDelayString = "否";
                    break;
                case "1":
                    isDelayString = "是";
                    break;
            }
        }
        return isDelayString;
    }

}
